from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, JsonResponse
from db.models import Customer
import json
from django.core.cache import cache  # redis
# 手机验证码
from utils import aliyunsms, restful
from utils.restful import *
# 邮箱验证
from django.core.mail import send_mail
from django.conf import settings


# 注册
class Register(View):
    """
    注册:
    """
    def post(self, request):
        data = json.loads(request.body, encoding='utf-8')
        if Customer.objects.filter(user_name=data['user_name']).count() > 0:
            return JsonResponse({"status": "404", "msg": "该用户已存在"})
        else:
            Customer.objects.create(
                user_name=data['user_name'],
                password=data['password'],
                phone=data['phone'],
                email=data['email']
            )
            return JsonResponse({"status": "200", "msg": "注册成功"})


# 登录
class Login(View):
    """
    登录
    """
    def post(self, request):
        data = json.loads(request.body, encoding='utf-8')
        if Customer.objects.filter(user_name=data['user_name']).count() < 0:
            return JsonResponse({"status": "200", "msg": "用户不存在"})
        else:
            if Customer.objects.get(user_name=data['user_name']).password == data['password']:
                return JsonResponse({"status": "200", "msg": "登录成功"})
            else:
                return JsonResponse({"status": "200", "msg": "密码错误"})


# 短信验证码
class PhoneCode:

    # 测试Redis存储
    def test_redis(self):
        cache.set('name', 'tom', 20)  # 存
        print(cache.has_key('name'))  # 判断
        print(cache.get('name'))  # 获取
        return HttpResponse("测试Redis")

    # 手机短信接口
    def sms_send(self, request):
        # http://127.0.0.1:8000/customer/send/
        # 获取手机号
        phone = request.GET.get('phone')
        phone = 15835934742
        # 生成6位验证码
        code = aliyunsms.get_code(6, False)
        # 缓存到Redis
        cache.set(phone, code, 60)  # 60s有效期
        print("判断是否存在:", cache.has_key(phone))
        print("获取Redis验证码:", cache.get(phone))
        # 发短信
        result = aliyunsms.send_sms(phone, code)
        return HttpResponse(result)

    # 短信验证码校验
    def sms_check(self, request):
        # 1.获取电话和手动输入的验证码
        phone = request.GET.get('phone')
        code = request.GET.get('code')
        phone = 15835934742
        code = 123456
        # 2.获取Redis中保存的code
        print('缓存中是否包含:', cache.has_key(phone))
        print("取值:", cache.get(phone))
        cache_code = cache.get(phone)
        # 3.判断
        if code == cache_code:
            return restful.ok("OK")
        else:
            return restful.page_error("验证码错误", data=None)


# 邮箱验证
class SendEmail:
    def send_email(self):
        title = "您的验证码为：980112"
        msg = "we together"
        email_from = settings.DEFAULT_FROM_EMAIL
        reciever = [
            "703675880@qq.com"
        ]
        # 发送邮件
        send_mail(title, msg, email_from, reciever)
        return JsonResponse({"status": "200", "msg": "email发送成功"})








